// ActionScript file
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;

import org.flexlancer.Logger.Logger;
import org.flexlancer.databases.Products;
import org.flexlancer.databases.dbServices;
import org.flexlancer.events.TypeInSearchItemSelected;

[Bindable]
private var txt_input:String = "";
[Bindable]
private var dpProducts:Array;

private var prev_list_index:Number = -1;
private var complete_list:Array;

private function OnCC():void{
	try{
		this.RefreshData();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function RefreshData():void{
	try{
		var arrProducts:Array = Products.List();
		
		this.complete_list = new Array();
		var insert:Object = new Object();
		var singlePrice:Object;
		
		for each ( var singleProd:Object in arrProducts ){
			for each ( singlePrice in singleProd.prices ){
				insert = new Object();
				insert.refid = singleProd.productid;
				insert.title = singleProd.title + " ("+singlePrice.title+")";
				insert.instock = singleProd.instock;
				insert.barcode = singleProd.barcode;
				insert.price = singlePrice.price;
				insert.type = "product";
				this.complete_list.push(insert);
			}
		}
		
		 var arrServices:Array = dbServices.List();
		for each ( var singleServ:Object in arrServices ){
			for each ( singlePrice in singleServ.prices ){
				insert = new Object();
				insert.refid = singleServ.serviceid;
				insert.title = singleServ.servicename + " ("+singlePrice.pricetype+")";
				insert.instock = "xxxxxx";
				insert.barcode = "xxxxxx";
				insert.price = singlePrice.price;
				insert.type = "service";
				this.complete_list.push(insert);
			}
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function ShowFilterData():void{
	try{
		this.dpProducts = new Array();
		
		var keyString:String = this.txtInput.text.toLowerCase();
		
		for each ( var singleProduct:Object in this.complete_list ){
			var field1:String = singleProduct.title.toLowerCase();
			
			if ( field1.indexOf(keyString) > - 1 ){
				this.dpProducts.push(singleProduct);
			}
		}
		
		this.dgProducts.dataProvider = this.dpProducts;
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnInput_Change():void{
	try{
		if ( this.txtInput.text.length == 0 ){
			this.dgProducts.visible = false;
		}else{
			this.dgProducts.visible = true;
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnInput_KeyUp(evt:KeyboardEvent):void{
	try{
		switch(evt.keyCode){
			case Keyboard.ESCAPE:
				this.txtInput.text = "";
				this.dgProducts.visible = false;
			break;
			case Keyboard.UP:
				if ( this.txtInput.text != ""){
					if ( this.dpProducts.length > 0 ){
						this.dgProducts.selectedIndex = this.dpProducts.length - 1;
						this.dgProducts.setFocus();
					}
				}
			break;
			case Keyboard.DOWN:
				if ( this.txtInput.text != "" ){
					if ( this.dpProducts.length > 0 ){
						this.dgProducts.selectedIndex = 0;
						this.dgProducts.setFocus();
					}
				}
			break;
			default:
				this.ShowFilterData();
			break;
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnDgProducts_KeyUp(evt:KeyboardEvent):void{
	try{
		switch(evt.keyCode){
			case Keyboard.UP:
				if ( this.prev_list_index == this.dgProducts.selectedIndex ){
					if ( this.dgProducts.selectedIndex == 0 ) {
						this.txtInput.setFocus();
						this.dgProducts.selectedIndex = -1;
						this.prev_list_index = -1
					}
				}
			break;
			case Keyboard.DOWN:
				if ( this.prev_list_index == this.dgProducts.selectedIndex ) {
					if ( this.dgProducts.selectedIndex == this.dpProducts.length - 1 ) {
						this.txtInput.setFocus();
						this.dgProducts.selectedIndex = -1;
						this.prev_list_index = -1;
					}
				}
			break;
			case Keyboard.ESCAPE:
				this.Reset();
			break;
			case Keyboard.ENTER:
				if ( this.dgProducts.selectedIndex > -1 ){
					this.dispatchEvent(new TypeInSearchItemSelected(this.dgProducts.selectedItem,TypeInSearchItemSelected.ITEM_SELECTED,true));
					this.Reset();
				}
			break;
		}
		this.prev_list_index = this.dgProducts.selectedIndex;
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function Reset():void{
	try{
		this.dgProducts.visible = false;
		this.txtInput.setFocus();
		this.txtInput.text = "";
		this.dgProducts.selectedIndex = -1;
	}catch(err:Error){
		Logger.LogE(err);
	}
}